package com.ark.cloud.dataservices.modules.sys.controller;

import com.ark.cloud.dataservices.common.web.entity.Result;
import com.ark.cloud.dataservices.modules.sys.entity.form.SeatForm;
import com.ark.cloud.dataservices.modules.sys.entity.po.Seat;
import com.ark.cloud.dataservices.modules.sys.service.ISeatService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/seat")
@Slf4j
public class SeatController {

    private final ISeatService seatService;

    public SeatController(ISeatService seatService) {
        this.seatService = seatService;
    }

    /**
     * 新增席位
     *
     * @param seatForm {@link SeatForm}
     * @return {@link Result}
     */
    @PostMapping
    public Result<?> add(@Valid @RequestBody SeatForm seatForm) {
        log.debug("name:{}", seatForm);
        if (StringUtils.isBlank(seatForm.getIpaddr())) {
            return Result.fail(50000, "席位 IP 不能为空");
        } else {
            boolean matches = seatForm.getIpaddr().matches("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$");
            if (!matches) {
                return Result.fail(50000, "IP 地址不合法");
            }
        }
        if (StringUtils.isBlank(seatForm.getLicense())) {
            return Result.fail(50000, "席位 License 不能为空");
        }
        if (StringUtils.isBlank(seatForm.getName())) {
            return Result.fail(50000, "席位名称不能为空");
        }
        Seat seat = seatForm.toPo(Seat.class);
        return Result.success(seatService.add(seat));
    }

    /**
     * 删除席位
     *
     * @param id 席位 id
     * @return {@link Result}
     */
    @DeleteMapping(value = "/{id}")
    public Result<?> delete(@PathVariable String id) {
        return Result.success(seatService.delete(id));
    }

    /**
     * 修改席位
     *
     * @param id       席位 id
     * @param seatForm {@link SeatForm}
     * @return {@link Result}
     */
    @PutMapping(value = "/{id}")
    public Result<?> update(@PathVariable String id, @Valid @RequestBody SeatForm seatForm) {
        log.debug("update with id:{}", id);
        Seat seat = seatForm.toPo(Seat.class);
        seat.setId(id);
        return Result.success(seatService.update(seat));
    }

    /**
     * 获取席位
     *
     * @param id 席位 id
     * @return {@link Result}
     */
    @GetMapping(value = "/{id}")
    public Result<?> get(@PathVariable String id) {
        log.debug("get with id:{}", id);
        return Result.success(seatService.get(id));
    }

    /**
     * 席位列表
     *
     * @return {@link Result}
     */
    @PostMapping("/list")
    public Result<?> list(@RequestBody SeatForm seatForm) {
        log.debug("query seats with param:{}", seatForm);
        QueryWrapper<Seat> seatQueryWrapper = new QueryWrapper<>();
        seatQueryWrapper.eq(StringUtils.isNotBlank(seatForm.getType()), "type", seatForm.getType());
        List<Seat> seatList = seatService.list(seatQueryWrapper);
        return Result.success(seatList);
    }
}